import { AppRouteObject } from "types/router";
import { ascend } from "ramda";
/**
 * return menu routes
 */
export const menuFilter = (items: AppRouteObject[]) => {
  return items
    .filter((item) => {
      const show = item.meta?.key;
      if (show && item.children) {
        item.children = menuFilter(item.children);
      }
      return show;
    })
    .sort(ascend((item) => item.order || Number.POSITIVE_INFINITY));
};

/**
 * 基于 src/router/routes/modules 文件结构动态生成路由
 */
export const getRoutesFromModules = () => {
  const modules = import.meta.glob("./modules/*.tsx", {
    eager: true,
  });
  const routes: AppRouteObject[] = [];
  for (const key in modules) {
    const mod = (modules as any)[key].default || {};
    const modList = Array.isArray(mod) ? [...mod] : [mod];
    routes.push(...modList);
  }
  return routes;
};
